package com.shoeshopping.action.pc;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shoeshopping.entity.GoodsItem;
import com.shoeshopping.entity.User;
import com.shoeshopping.service.pc.GetIntoCartService;
import com.shoeshopping.service.pc.GetIntoOrderService;
import com.shoeshopping.utils.pc.Result;

@Controller("goodsItemAction")
public class GoodsItemAction extends ActionSupport {

	private GoodsItem goodsItem;
	
	@Resource(name="getIntoCartService")
	GetIntoCartService getIntoCartService;
	
	@Resource(name="getIntoOrderService")
	GetIntoOrderService getIntoOrderService;

	public GetIntoOrderService getGetIntoOrderService() {
		return getIntoOrderService;
	}

	public void setGetIntoOrderService(GetIntoOrderService getIntoOrderService) {
		this.getIntoOrderService = getIntoOrderService;
	}

	public GoodsItem getGoodsItem() {
		return goodsItem;
	}

	public void setGoodsItem(GoodsItem goodsItem) {
		this.goodsItem = goodsItem;
	}
	
	public GetIntoCartService getGetIntoCartService() {
		return getIntoCartService;
	}

	public void setGetIntoCartService(GetIntoCartService getIntoCartService) {
		this.getIntoCartService = getIntoCartService;
	}
	
	public String showCart() {
		
		getIntoCartService.showCart();
		
		return Result.SUCCESS;
	}

	public String addToCart() {
		
		System.out.println("+++++++++goodsid = " + goodsItem.getGoods().getId());
		getIntoCartService.save(goodsItem);
		Map Session = ActionContext.getContext().getSession();
		User u = (User) Session.get("user");
		List<GoodsItem> giList = getIntoCartService.find(u.getShoppingCart().getId());
		if(Session.get("goodsItemList") != null) {
			Session.remove("goodsItemList");
		}
		Session.put("goodsItemList", giList);
		
		return Result.SUCCESS;
	}
	
	public String update() {
		
		getIntoCartService.update(goodsItem);
		
		return Result.SUCCESS;
	}
	
	public String addToOrder() {
		
		getIntoCartService.addToOrder(goodsItem);
		
		return Result.SUCCESS;
	}
	
	public String Delete() {
		
		getIntoCartService.delete(goodsItem);
		/*
		Map Session = ActionContext.getContext().getSession();
		List<GoodsItem> giList = getIntoCartService.find(goodsItem.getShoppingCart().getId());
		if(Session.get("goodsItemList") != null) {
			Session.remove("goodsItemList");
		}
		Session.put("goodsItemList", giList);
		*/
		return Result.DELETE_IN_CARTPAGE;
	}
	
	public String find() {
		
		String shoppingCartId = goodsItem.getShoppingCart().getId();
		
		List list = getIntoCartService.find(shoppingCartId);
		
		Map Session = ActionContext.getContext().getSession();
		
		Session.put("goodsItemList", list);
		
		return Result.SUCCESS;
	}
	
}
